package com.jn.gsondemo.views;

import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;

import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;

import com.google.gson.Gson;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import com.google.gson.reflect.TypeToken;
import com.jn.gsondemo.R;
import com.jn.gsondemo.entity.Data;
import com.jn.gsondemo.entity.People;
import com.jn.gsondemo.entity.School;
import com.jn.gsondemo.entity.Student;
import com.jn.gsondemo.entity.User;
import com.jn.gsondemo.manager.UserManager;
import com.jn.gsondemo.utils.JsonUtils;

import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.inject.Inject;

import dagger.hilt.android.AndroidEntryPoint;

@AndroidEntryPoint
public class MainActivity extends AppCompatActivity {

    private Student mStudent;
    private Student mStudent1;

    // 注入Gson实例
    @Inject
    Gson gson;

    // 注入仓库实例
    @Inject
    UserManager mUserManager;

    private TextView textView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EdgeToEdge.enable(this);
        setContentView(R.layout.activity_main);
        ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main), (v, insets) -> {
            Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars());
            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom);
            return insets;
        });
        textView = findViewById(R.id.text);
        //objToJson();
        //jsonToObj();
        //objToJson2();
        //jsonToObj2();
        //jsonToObj3();
        //jsonToList();
        //testJsonArray();
        //getValueByKey();
        //testJsonParser();
        demoGsonUsage();
    }

    private void demoGsonUsage() {
        // 1. 从网络获取JSON并解析为对象
        String jsonFromNetwork = mUserManager.fetchUserJsonFromNetwork(this);
        User user = mUserManager.convertJsonToUser(jsonFromNetwork);
        textView.setText("从JSON解析的用户:\n" +
                "姓名: " + user.getName() + "\n" +
                "年龄: " + user.getAge() + "\n" +
                "注册日期: " + user.getRegisterDate() + "\n\n");

        // 2. 创建新用户并序列化为JSON
        User newUser = new User("李四", 30, new Date());
        String newUserJson = mUserManager.convertUserToJson(newUser);
        textView.append("用户对象转换的JSON:\n" + newUserJson);
    }

    /**
     * 把实体对象转化为json字符串
     *
     * @return
     */
    private String objToJson() {
        mStudent = new Student();
        mStudent.setStudent_code("22");
        mStudent.setStudent_id(1);
        mStudent.setStudent_name("刘老八");
        mStudent.setUsername("abcdef");
        mStudent.setPassword("123456");
        mStudent.setClass_name("计算机10班");
        //Gson gson = new Gson();
        String jsonStr = gson.toJson(mStudent);
        Log.d("asdf", "objToJson : " + jsonStr);
        return jsonStr;
    }

    /**
     * json字符串转化为实体对象
     */
    private void jsonToObj() {
        String jsonStr = objToJson();
        //Gson gson = new Gson();
        mStudent1 = gson.fromJson(jsonStr, Student.class);
        Log.d("asdf", "jsonToObj = " + mStudent1.toString());
    }

    /**
     * 复杂对象转化为json字符串
     */
    private void objToJson2() {
        Student student = new Student();
        student.setStudent_code("22");
        student.setStudent_id(1);
        student.setStudent_name("刘老八");
        student.setUsername("abcdef");
        student.setPassword("123456");
        student.setClass_name("计算机10班");

        Student student2 = new Student();
        student2.setStudent_code("22");
        student2.setStudent_id(1);
        student2.setStudent_name("刘老八");
        student2.setUsername("abcdef");
        student2.setPassword("123456");
        student2.setClass_name("计算机10班");

        List<Student> students = new ArrayList<>();
        students.add(student);
        students.add(student2);

        School school = new School();
        school.setName("吉林农业科技学院");
        school.setAddress("吉林市");
        school.setStudents(students);

        Gson gson = new Gson();
        String jsonStr = gson.toJson(school);
        Log.d("asdf", "school = " + jsonStr);
    }

    /**
     * 复杂json转化为实体对象
     */
    private void jsonToObj2() {
        String jsonStr = getJson("school.json");
        Gson gson = new Gson();
        School school = gson.fromJson(jsonStr, School.class);
        Log.d("asdf", "jsonToObj = " + school.toString());
    }

    private void jsonToObj3() {
        String jsonStr = getJson("student_list.json");
        Gson gson = new Gson();
        Data student_list = gson.fromJson(jsonStr, Data.class);
        Log.d("asdf", "jsonToObj = " + student_list.getData().getGrade2021());
    }

    /**
     * json数组转化为List
     */
    private void jsonToList() {
        Gson gson = new Gson();
        String jsonStr = getJson("peoples.json");
        Type type = new TypeToken<List<People>>() {}.getType();
        List<People> list = gson.fromJson(jsonStr, type);

        //List<People> list = JsonUtils.jsonToList(jsonStr, People.class);

        for(People people : list) {
            Log.d("asdf", "people = " + people.toString());
        }
    }

    /**
     * 创建json数组
     */
    private void testJsonArray() {
        JsonArray jsonArray = new JsonArray();
        jsonArray.add("老张");
        jsonArray.add("老李");
        jsonArray.add("老王");
        String jsonStr = jsonArray.toString();
        Log.d("asdf", "json array = " + jsonStr);
    }

    /**
     * 通过json中的任意key获取对应value
     */
    private void getValueByKey() {
        String jsonStr = getJson("news.json");
        String value = JsonUtils.getKeyRecursively(jsonStr, "date");
        Log.d("asdf", "getValueByKey value ==== " + value);
    }

    /**
     * JsonParser
     */
    private void testJsonParser() {
        String jsonStr = getJson("student_list.json");
        JsonObject jo = JsonParser.parseString(jsonStr).getAsJsonObject();
        JsonObject data = jo.get("data").getAsJsonObject();
        JsonArray array = data.get("grade2021").getAsJsonArray();
        for (JsonElement jsonElement : array) {
            String name = jsonElement.getAsJsonObject().get("student_name").getAsString();
            Log.d("asdf", " ==name== " + name);
        }
    }

    private String getJson(String name) {
        String json = JsonUtils.getJsonFromAssets(this, name);
        Log.d("asdf", "getJson = " + json);
        return json;
    }
}